from __future__ import unicode_literals
import requests
from bs4 import BeautifulSoup
import time
import json

base_url='http://m.lovefoux.com'

headers = {'Accept': 'text/html, application/xhtml+xml, image/jxr, */*',
               'Accept - Encoding':'gzip, deflate',
               'Accept-Language':'zh-Hans-CN, zh-Hans; q=0.5',
               'Connection':'Keep-Alive',
               'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'}
imgurl_list=[]
count=0
mark=True
url='http://m.lovefoux.com/gaoxiao/50741.html'
url_history=''
while(mark):
    try:

        r = requests.get(url,timeout=5)
        content=r.content
        soup = BeautifulSoup(content,"lxml")



        


        a_set=soup.find('div',attrs={'class':'pagearti'}).find_all('a')


        #只有当找不到下页标签和链接重复时才推出循环
        mark=False
        for a in a_set:
            if '下页' in a.text:
                url_history=url
                url=base_url+a['href']
                mark=True
                break
        
        if url==url_history:
            mark=False

        pa=soup.find('div',attrs={'class':'cont'})
        img=pa.find('img')

        imginfo=[]
        if(img['src'] not in imgurl_list):
            imginfo.append(img['src'])
            imginfo.append(img["alt"])
            imgurl_list.append(imginfo)
            count=count+1


        if(count>200):
            print('strat write to disk...'+str(count))
            with open('lovefou_gaoxiao.json',mode='w+',encoding='utf-8') as file:
                file.write(json.dumps(imgurl_list,ensure_ascii=False))
            count=0


    except Exception as e:
            print (e)
            print(count)
            print(url)
    finally:
        time.sleep(1)
        print(str(count),end='\r')

print('退出url'+url)
print('strat write to disk...'+str(count))
with open('lovefou_gaoxiao.json',mode='w+') as file:
    file.write(json.dumps(imgurl_list,ensure_ascii=False))

# count=0
# for imgurl in imgurl_list[:10]:
#     print(imgurl)
#     r = requests.get(imgurl,headers=headers)
#     count=count+1
#     with open(str(count)+'.gif','wb') as file:
#         file.write(r.content)
